#!/usr/bin/env python3

import sys
import string
import csv
import os
from itertools import islice

REF_FILE = sys.argv[1]
OBJ = sys.argv[2]

class SymbolInfo:
    def __init__(self):
        self.symbol = ''
        self.defined_by_obj = ''
        self.defined_by_archive = ''
        self.reference_by_obj = ''
        self.reference_by_archive = ''

def get_object(content):
    begin = content.find("(")
    if begin == -1:
        return content
    return content[begin + 1:len(content) - 1]

def get_archive(content):
    begin = content.find("(")
    if begin == -1:
        return ""
    return content[0:begin]

found = False
symbol_list = []
with open(REF_FILE, 'r') as file_obj:
    for line in islice(file_obj, 0, None):
        line = line.strip()
        if found == False and line == 'Cross Reference Table' :
            print("find Cross Reference Table")
            found = True
        if found :
            content = line.split()
            if len(content) != 2 and len(content) != 1:
                continue
            if content[0] == 'Symbol' and content[1] == 'File':
                continue
            if len(content) == 2:
                sym = SymbolInfo()
                symbol_list.append(sym)
                sym.symbol = content[0]
                sym.defined_by_obj = get_object(content[1])
                sym.defined_by_archive= get_archive(content[1])
            if len(content) == 1:
                sym.reference_by_obj = get_object(content[0])
                sym.reference_by_archive = get_archive(content[0])

print(OBJ + " reference:")
for sym in symbol_list:
    if sym.reference_by_archive == OBJ and sym.defined_by_archive != OBJ:
        print("use " + sym.symbol + " defined by " + sym.defined_by_obj + " " + sym.defined_by_archive)